package com.zyy.java8.samples.stream;

import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * @author zhangyiying
 * @ClassName: Streams5
 * @Description:
 * @date 2021/12/28 9:47
 */
public class Streams5 {

    public static void main(String[] args) {
        List<String> strings = Arrays.asList("d2", "a2", "b1", "b3", "c");

//        test1(strings);
//        test2(strings);
//        test3(strings);
//        test4(strings);
//        test5(strings);
//        test6(strings);
//        test7(strings);
        test8(strings);
    }


    /**
     * 把它放在Supplier中在执行后不会关闭
     * @param stringCollection
     */
    private static void test8(List<String> stringCollection){
        Supplier<Stream<String>> streamSupplier  = () -> stringCollection
                .stream()
                .filter(s -> s.startsWith("a"));

        streamSupplier.get().anyMatch(s -> true);
        streamSupplier.get().noneMatch(s -> true);
    }

    /**
     * stream has already been operated upon or closed
     * stream 当进行任意一次操作(map,anyMatch,noneMatch)之后就会关闭
     * @param stringCollection
     */
    private static void test7(List<String> stringCollection){
        Stream<String> stream = stringCollection
                .stream()
                .filter(s -> {
                    System.out.println("filter = " + s);
                    return s.startsWith("a");
                });

        stream.anyMatch(s -> {
            System.out.println("anyMatch = " + s);
            return true;
        });

        stream.noneMatch(s -> true);
    }

    /**
     * anyMatch 当有一个匹配到后直接结束流
     * @param stringCollection
     */
    private static void test6(List<String> stringCollection){
        stringCollection
                .stream()
                .map(s -> {
                    System.out.println("map = " + s);
                    return s.toUpperCase();
                })
                .anyMatch(s -> {
                    System.out.println("anyMatch = " + s);
                    return s.startsWith("A");
                });
    }


    /**
     * 若filter在sorted之前执行则先把所有元素都进行过滤之后再进行排序
     * @param stringCollection
     */
    private static void test5(List<String> stringCollection){
        stringCollection
                .stream()
                .filter(s -> {
                    System.out.println("filter = " + s);
                    return s.startsWith("a");
                })
                .sorted((s1, s2) -> {
                    System.out.printf("sorted: %s; %s \n", s1, s2);
                    return s1.compareTo(s2);
                })
                .map(s -> {
                    System.out.println("map = " + s);
                    return s.toUpperCase();
                })
                .forEach(s -> {
                    System.out.println("forEach = " + s);
                });
    }


    private static void test4(List<String> stringCollection) {
        stringCollection
                .stream()
                .sorted((s1, s2) -> {
                    System.out.printf("sorted: %s; %s \n", s1, s2);
                    return s1.compareTo(s2);
                })
                .filter(s -> {
                    System.out.println("filter = " + s);
                    return s.startsWith("a");
                })
                .map(s -> {
                    System.out.println("map = " + s);
                    return s.toUpperCase();
                })
                .forEach(s -> {
                    System.out.println("forEach = " + s);
                });
    }

    private static void test3(List<String> stringCollection) {
        stringCollection
                .stream()
                .filter(s -> {
                    System.out.println("filter = " + s);
                    return s.startsWith("a");
                })
                .map(s -> {
                    System.out.println("map = " + s);
                    return s.toUpperCase();
                })
                .forEach(s -> {
                    System.out.println("forEach = " + s);
                });
    }


    private static void test2(List<String> stringCollection) {
        stringCollection
                .stream()
                .map(s -> {
                    System.out.println("map: " + s);
                    return s.toUpperCase();
                })
                .filter(s -> {
                    System.out.println("filter： " + s);
                    return s.startsWith("A");
                })
                .forEach(s -> System.out.println("forEach: " + s));
    }

    private static void test1(List<String> stringCollection) {
        stringCollection
                .stream()
                .filter(s -> {
                    System.out.println("filter: " + s);
                    return true;
                })
                .forEach(s -> System.out.println("forEach: " + s));
    }
}
